<?php
defined('_JEXEC') or die('Restricted access');
$app = JFactory::getApplication();
$input = $app->input;
$action = $input->get('action');
$items = $this->get('Items');
$this->pagination = $this->get('pagination');
$getArray = $app->input->getArray();
?>

<?php if($action == 'create'): ?>
	<link href="/templates/ppspconsole/css/select2.min.css" type="text/css" rel="stylesheet" />
    <link href="/templates/ppspconsole/css/jquery.range.css" type="text/css" rel="stylesheet" />
	<?php
	$applist = $getArray['applist'];
	?>
<div class="res-title-rg">
    <span class="fl pos-icon text-c"><b class="glyphicon glyphicon-plus fontsize20"></b></span>
    <div class="title-info">
        <h2 class="fontsize20">New Campaign</h2>
        <p><a href="#">Home</a>&nbsp;/&nbsp;Setup&nbsp;/&nbsp;New Campaign</p>
    </div>
</div>
	<form action="/index.php?option=com_console&view=campaigntracking&Itemid=127&task=CampaignTracking&action=store" method="post" class="form-horizontal J_ajaxFsorm" >
<div class="ws-content">
    	<div class="pdall15 bgwhite" style="width:820px; margin:0 auto;">
    		<div class="mobitems">
    			<span class="fl mobiname fontsize14">&nbsp;</span>
    			<div class="fl fontsize20">BASE</div>
    			<div class="clear"></div>
    		</div>
    		<div class="mobitems pos_pre">
    			<span class="fl mobiname fontsize14 lg36">Application</span>
    			<div class="pos_pre">
    				<select class="select2" name="app_id" id="appId" data-url="/index.php?option=com_console&view=campaigntracking&Itemid=127&task=CampaignTracking&action=ajax">
						<?php foreach($applist as $app):?>
                      <option value="<?php echo $app->id?>"><?php echo $app->name?></option>
                     <?php  endforeach;?>
                    </select>
    			</div>
    			<div class="clear"></div>
    		</div>
    		<div class="mobitems pos_pre">
    			<span class="fl mobiname fontsize14 lg36">Name</span>
    			<div class="fl pos_pre">
    				<input type="text" name="name" class="selecshow fontsize16 txt-fd" placeholder="" id="selectext" style="background:none;" />
    			</div>
    			<div class="clear"></div>
    		</div>


            <div class="mobitems pos_pre" style="padding:20px 0;">
                <span class="fl mobiname fontsize14">Click Attribution Lookback Window</span>
                <div class="fl" style="width:300px;">
                    <input type="hidden" class="daystext" value="23" />
                    <div class="single-slider"></div>
                </div>
                <div class="clear"></div>
            </div>


            <div class="mobitems">
                <span class="fl mobiname fontsize14">Tracking URL</span>
				<?php
				$open_id = date('Ymd').rand(1000,9999).date('s');
				$mobiurl = "http://www.adgyde.com/b/1/r?c={$open_id}";
				?>
				<input name="open_id" type="hidden" value="<?php echo $open_id?>">
                <div class="fl mobiurl" style="height: 100px;"><?php echo $mobiurl?></div>
                <div class="clear"></div>
            </div>
            <div class=" bgwhite fontsize14">

                <div class="text-r"><button type="submit" style="border: 0;" class="btn-popup colwhite fontsize16 J_ajax_submit_btn">Save & Close</button></div>

            </div>
    	</div>
</div>
	</form>
	<script type="text/javascript">
		$(function(){
			var appid = $('#appId').val();
			var appurl = $('#appId').data('url');
			getEvent(appid,appurl);

			$('#appId').change(function () {
				var appid = $(this).val();
				var appurl = $('#appId').data('url');
				getEvent(appid,appurl);
			});

		});

		function getEvent(appid,url) {
			if (appid > 0) {
				$("#addsearch").empty();
				var url = url+'&appid='+appid;
				$.getJSON(url, function (data) {
					$.each(data,function(i,v){
						$("#addsearch").append("<option value='"+ v.event_id+"'>"+ v.event_name+"</option>");
					})
				});
			}
		}
	</script>
<?php else: //list
	$model = $this->getModel();
	?>
<div class="res-title-rg">
    <span class="fl pos-icon text-c"><i class="setup"></i></span>
    <div class="title-info">
        <h2 class="fontsize20">Campaign Tracking</h2>
        <p><a href="#">Home</a>&nbsp;/&nbsp;Setup&nbsp;/&nbsp;Campaign Tracking</p>
    </div>
</div>
<div class="ws-content pdall15">
	<div class="camtab tymgbt">
		<!-- <nav>
		  <ul class="pagination navmg fl">
		    <li><a href="#">Copy</a></li>
		    <li><a href="#">Excel</a></li>
		    <li><a href="#">CSV</a></li>
		    <li><a href="#">PDF</a></li>
		  </ul>
		</nav> -->
		<a href="/index.php?option=com_console&view=campaigntracking&Itemid=127&task=CampaignTracking&action=create"  data-target=".add-new-campaign" class="fr btn-popup fontsize16">+&nbsp;New Campaign</a>
        <div class="clear"></div>
	</div>
	<table class="table table-bordered table-striped">
      <thead>
        <tr class="fontsize14">
          <th>#</th>
          <th>Name</th>
          <th>Openid</th>
          <th>Create Date</th>
          <th>Click Look Back Window</th>
          <th>Channel</th>
        </tr>
      </thead>
      <tbody>
	  <?php if($items):?>
	  <?php foreach ($items as  $item): ?>
        <tr data-toggle="modal" data-target=".bs-example-modal-lg">
          <td><?php echo $item->id?></td>
			<td><?php echo $item->name?></td>
			<td><?php echo $item->open_id?></td>

			<td><?php echo $item->create_date?></td>
			<td><?php echo $item->lookback_window ?> Days</td>
			<td>
				<a href="/index.php?option=com_console&view=campaignchannel&Itemid=127&task=CampaignChannel&action=create&campaign_id=<?php echo $item->id?>&app_id=<?php echo $item->app_id?>"> <span class="glyphicon glyphicon-plus"></span> </a> <br>
				<?php
				$campaign_channel = $model->getCampaignChannel( $item->id);
			  if($campaign_channel):
				foreach($campaign_channel as $key => $val):
				?>

				<?php echo $val->name?>  <a href="/index.php?option=com_console&view=campaignchannel&Itemid=127&task=CampaignChannel&action=edit&campaign_id=<?php echo $item->id?>&app_id=<?php echo $item->app_id?>&id=<?php echo $val->id?>"> <span class="glyphicon glyphicon-pencil"></span></a> <br>
				<?php endforeach;?>
			  <?php endif;?>
			</td>
        </tr>
       <?php endforeach;?>
	  <?php endif;?>
      </tbody>
    </table>
	<div class="pagination">
		<?php echo $this->pagination->getPagesLinks(); ?>
	</div>
</div>
<?php endif; //endlist?>
<script type="text/javascript" src="/templates/ppspconsole/js/select2.min.js"></script>
<script type="text/javascript" src="/templates/ppspconsole/js/jquery.range.js"></script>
<script type="text/javascript" src="/templates/ppspconsole/js/zh-CN.js"></script>
<script>
$(function(){
    $('.single-slider').jRange({
        from: 0,
        to: 30,
        step: 1,
        scale: [0,3,6,9,12,15,18,21,24,27,30],
        format: '%s',
        width: 480,
        showLabels: true,
        snap: true
    });
    $("select").select2();
	$(".eventile span").click(function(){
		if($(this).attr("class")=="selected"){
			$(this).removeClass();
            $(".enable-content").fadeOut();
            $(this).parent().find("input[name='enable_sync']").val("0");
            $(".eventSync-nr").fadeOut();
		}else{
			$(this).addClass("selected");
            $(".enable-content").fadeIn();
            $(this).parent().find("input[name='enable_sync']").val("1");
            $(".eventSync-nr").fadeIn();
		}
	});
    $("#cost").click(function(){
        if($(this).attr("class")=="selected"){
            $(this).removeClass();
            $(this).parent().find("input[name='cost']").val("0");
            $(this).parent().find("input[name='cost_src']").val("0");
            $(this).parent().find("input[name='cost']").removeAttr("disabled");
        }else{
            $(this).addClass("selected");
            $(this).parent().find("input[name='cost']").val("-1");
            $(this).parent().find("input[name='cost_src']").val("1");
            $(this).parent().find("input[name='cost']").prop("disabled","true");
        }
    })
	var MaxInputs       = 6; //最多可以添加几行
	var InputsWrapper   = $("#InputsWrapper"); //添加到哪里
	var AddButton       = $("#addmobisoc"); //点击添加按钮id
	var x = InputsWrapper.length;
	var FieldCount=1;

	$(AddButton).click(function(e){
        var addsearch = $("#addsearch").find("option:selected").text();
        var addsearchval = $("#addsearch").val();
        var addeventag = $("#addeventag").val();
		if(x <= MaxInputs)
		{
			FieldCount++;
			$(InputsWrapper).append('<tr><td>'+addsearch+'<input type="hidden" name="map[event_id][]" value='+addsearchval+' /></td><td>'+addeventag+'<input type="hidden" name="map[event_tag][]" value='+addeventag+' /></td><td><a href="javascript:void(0)" class="removetflist glyphicon glyphicon-remove colred"></a></td></tr>');
			x++;
		}
		//return false;
	});

	$("body").on("click",".removetflist", function(e){
		FieldCount--;
		$(this).parent().parent('tr').remove(); //删除当前点击的div
		x--;
		return false;
	});
	// bshow();
	// $(".days-tips span").click(function(){
	// 	var index = $(this).index();
	// 	$(this).addClass('selected1');
	// 	$(".days-tips").find("b").hide();
	// 	bshow();
	// 	$(".carrow").animate({left:(index-1)*16-3})
 //        $(".carrow").find("font").text(index);
	// 	// $(this).find("b").show();
	// 	$(this).nextAll().removeClass('selected');
 //        $(this).nextAll().removeClass('selected1');
 //        $(this).prevAll().removeClass('selected1');
	// 	$(this).prevAll().addClass('selected');
	// 	$("#daystext").val(index);
	// });
	$(".f-selct-content p").click(function(){
		$("#selectext").val($(this).text());
	});
	$(".f-selct-content-ad p").click(function(){
		$("#networktxt").val($(this).text());
	});
})
// function bshow(){
//     $(".days-tips").find("b").eq(0).show();
//     $(".days-tips").find("b").eq(2).show();
//     $(".days-tips").find("b").eq(6).show();
//     $(".days-tips").find("b").eq(13).show();
//     $(".days-tips").find("b").eq(29).show();
// }
</script>